package output;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import base.Game;

import commandProcess.I_CommandProcess;

@SuppressWarnings("serial")
public class MyJFrame extends JFrame implements I_Output{
	
	private final int WIDTH_SHIFT = 8;
	private final int HEIGHT_SHIFT = 30;
	private final int DEFAULT_X_POSITION = 200;
	private final int DEFAULT_Y_POSITION = 10;
	private final int BUTTON_RELEASED = -1;
	
	private I_CommandProcess _command;
	private BufferedImage _bufferImage;
	
	public MyJFrame(I_CommandProcess command)
	{
		_command = command;
		
		setSize(Game.DEFAULT_BIG_ELEMENT_SIZE * Game.DEFAULT_NUMBER_OF_BIG_ITEMS + WIDTH_SHIFT * 2,
				Game.DEFAULT_BIG_ELEMENT_SIZE * Game.DEFAULT_NUMBER_OF_BIG_ITEMS + HEIGHT_SHIFT + WIDTH_SHIFT);
		setLocation(DEFAULT_X_POSITION, DEFAULT_Y_POSITION);
		setVisible(true);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				_command.takeCommand(e.getKeyCode() * BUTTON_RELEASED);
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
					System.exit(0);
				_command.takeCommand(e.getKeyCode());
			}
		});
		
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				_command.takeCommand(KeyEvent.VK_SPACE * BUTTON_RELEASED);
			}
		});
	}
	
	public void setBufferedImage(BufferedImage bufferImage)
	{
		_bufferImage = bufferImage;
	}
	
	@Override
 	public void paint(Graphics g) {
		g.drawImage(_bufferImage, WIDTH_SHIFT, HEIGHT_SHIFT, null);
	}
}